import numpy as np
class StateRecognizer:
    def __init__(self):
        # 加速度X轴正向阈值，用于识别加速状态
        self.ACCELERATION_X_THRESHOLD_POS = 0.1
        # 加速度X轴负向阈值，用于识别减速状态
        self.ACCELERATION_X_THRESHOLD_NEG = -0.1
        # 陀螺仪Z轴方差阈值，用于识别转弯状态
        self.GYROSCOPE_Z_VARIANCE_THRESHOLD = 0.01
        # 加速度方差阈值，用于识别平缓行驶状态
        self.ACCELERATION_VARIANCE_THRESHOLD = 0.01

    def recognize_states(self, window_data):
        """
        识别单个时间窗口的行驶状态

        参数:
            window_data: 包含以下字段的字典:
                - acceleration_x: X轴加速度数组
                - acceleration_y: Y轴加速度数组
                - acceleration_z: Z轴加速度数组
                - gyroscope_z: Z轴陀螺仪数据数组

        返回:
            状态字符串: "加速", "减速", "转弯", "平缓行驶" 或组合状态
        """
        # 计算时域特征
        acc_x_mean = np.mean(window_data['acceleration_x'])
        acc_x_variance = np.var(window_data['acceleration_x'])
        acc_y_variance = np.var(window_data['acceleration_y'])
        acc_z_variance = np.var(window_data['acceleration_z'])
        gyro_z_variance = np.var(window_data['gyroscope_z'])

        # 初始化状态列表
        states = []

        # 检测加速或减速
        if acc_x_mean > self.ACCELERATION_X_THRESHOLD_POS:
            states.append("加速")
        elif acc_x_mean < self.ACCELERATION_X_THRESHOLD_NEG:
            states.append("减速")

        # 检测转弯
        if gyro_z_variance > self.GYROSCOPE_Z_VARIANCE_THRESHOLD:
            states.append("转弯")

        # 检测平缓行驶
        if (acc_x_variance < self.ACCELERATION_VARIANCE_THRESHOLD and
            acc_y_variance < self.ACCELERATION_VARIANCE_THRESHOLD and
            acc_z_variance < self.ACCELERATION_VARIANCE_THRESHOLD):
            states.append("平缓行驶")

        # 如果没有其他状态满足，则标记为匀速行驶
        if not states:
            return "匀速行驶"

        return "+".join(states)


    def process_and_recognize(self, all_windows_data):
        """
        处理多个时间窗口的数据并进行状态识别

        参数:
            all_windows_data: 包含多个窗口数据的列表

        返回:
            包含识别结果的列表
        """
        results = []
        for window_data in all_windows_data:
            state = self.recognize_states(window_data)
            results.append(state)
        return results

# 示例使用
if __name__ == "__main__":
    # 创建示例数据
    window_data_1 = {
        'acceleration_x': np.random.normal(0.3, 0.1, 100),  # 加速数据
        'acceleration_y': np.random.normal(0, 0.05, 100),
        'acceleration_z': np.random.normal(0, 0.05, 100),
        'gyroscope_z': np.random.normal(0, 5, 100)  # 不转弯数据
    }

    window_data_2 = {
        'acceleration_x': np.random.normal(0, 0.05, 100),  # 匀速数据
        'acceleration_y': np.random.normal(0, 0.05, 100),
        'acceleration_z': np.random.normal(0, 0.05, 100),
        'gyroscope_z': np.random.normal(0, 60, 100)  # 转弯数据
    }

    window_data_3 = {
        'acceleration_x': np.random.normal(0, 0.05, 100),  # 平缓行驶数据
        'acceleration_y': np.random.normal(0, 0.05, 100),
        'acceleration_z': np.random.normal(0, 0.05, 100),
        'gyroscope_z': np.random.normal(0, 5, 100)
    }

    # 创建识别器实例
    recognizer = StateRecognizer()

    # 进行状态识别
    print("Window 1 State:", recognizer.recognize_states(window_data_1))
    print("Window 2 State:", recognizer.recognize_states(window_data_2))
    print("Window 3 State:", recognizer.recognize_states(window_data_3))
